package com.mysoft.gfyx.common.message.impl;

import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import com.mysoft.framework.common.exception.BusinessLogicException;
import com.mysoft.gfyx.common.message.EmailHelper;
import com.mysoft.gfyx.common.message.MipService;
import com.mysoft.gfyx.common.message.dto.CommonSendPushOrderRequestDto;
import com.mysoft.gfyx.common.message.dto.YcgApiBaseRespondDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmailHelperImpl implements EmailHelper {

    @Value("${email.host}")
    private String host;

    @Value("${email.port}")
    private String port;

    @Value("${email.from}")
    private String from;

    @Value("${email.username}")
    private String username;

    @Value("${email.password}")
    private String password;

    @Autowired(required = false)
    private MipService mipService;

    public void sendEmail(List<String> addresses, String title, String html) {

        MailAccount account = new MailAccount();
        account.setHost(host);
        account.setPort(Integer.valueOf(port));
        account.setAuth(true);
        account.setFrom(from);
        account.setUser(username);
        account.setPass(password);
        // 使用SSL安全连接
        account.setSslEnable(true);


        MailUtil.send(account, addresses, title, html, true);
    }

    public void sendEmailByYcg(CommonSendPushOrderRequestDto dto) {
        //调用云采购消息中心发送消息接口
        if (dto.getEmail() != null) {
            YcgApiBaseRespondDto sendRes = mipService.sendPushOrder(dto);
            if (!sendRes.getSuccess()) {
                throw new BusinessLogicException("发送邮件失败:" + sendRes.getMessage());
            }
        }
    }


}
